package ro.gateway.aida.autodates;

import java.sql.SQLException;
import java.util.TimerTask;

import javax.servlet.ServletContext;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.Activity;
import ro.gateway.aida.obj.ActivityStatus;
import ro.gateway.aida.obj.db.ActivityStatusDB;

/**
 * <p>Title: Romanian AIDA</p> <p>Description: </p> <p>Copyright: Copyright (comparator) 2003</p> <p>Company:
 * ro-gateway</p>
 *
 * @author Mihai Popoaei, smike@intellisource.ro
 * @version 1.0-* @version $Id: StatusAutomation\044Task.java,v 1.1 2004/10/24 23:37:15 mihaipostelnicu Exp $
 */
public class StatusAutomation$Task extends TimerTask {
	ServletContext application;
	boolean running = false;

	public StatusAutomation$Task(ServletContext application) {
		super();
		this.application = application;
	}

	public boolean isRunning() {
		return running;
	}

	public void run() {
		synchronized (this) {
			if (this.running) return;
			this.running = true;
		}
		System.out.println("StatusAutomationTask started");
		long time = System.currentTimeMillis();

		PersistenceToken token = PersistenceManager.tokenLookup(application);
		ActivityForStatus$Proxy$DB pers_man = ActivityForStatus$Proxy$DB.getManager(token);
		try {
			ActivityForStatus$Proxy[] activities = pers_man.getAll();
			if (activities != null) {
				for (int i = 0; i < activities.length; i++) {
					ActivityForStatus$Proxy activity = activities[i];
					activity.setStatus(getStatusByDates(activity, token));
//				System.out.println(activity.getId());
//				try {
//					Thread.sleep(500);
//				} catch (InterruptedException e) {
//					e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//				}
				}
				pers_man.updateStatusForActivities(activities);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		synchronized (this) {
			this.running = false;
		}
		System.out.println("StatusAutomationTask ended. Job done in :" + (System.currentTimeMillis() - time) + " milliseconds");
	}

	public static ActivityStatus getStatusByDates(ActivityForStatus$Proxy activity, PersistenceToken token) {
		ActivityStatusDB asDB = ActivityStatusDB.getManager(token);
		ActivityStatus planned = null;
		ActivityStatus commited = null;
		ActivityStatus ongoing = null;
		ActivityStatus completed = null;

		try {
			planned = asDB.getByStCode(1);
			ongoing = asDB.getByStCode(2);
			completed = asDB.getByStCode(3);
			commited = asDB.getByStCode(6);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		if (planned == null || ongoing == null || completed == null || commited == null) {
			System.err.println("StatusAutomation$Task.updateStatusByDates: Harmonized Statuses WRONG! (1=planned, 2=ongoing, 3=completed, 6=commited)");
			return null;
		}

		ActivityStatus status = planned;

		long current_date = System.currentTimeMillis();
		if (activity.getDate_ae() <= current_date) {
			return completed;
		}

		if (activity.getDate_as() <= current_date) {
			return ongoing;
		}

		if (activity.getDate_aa() <= current_date) {
			return commited;
		}

		return planned;
	}

	public static ActivityStatus getStatusByDates(Activity activity, PersistenceToken token) {
		ActivityStatusDB asDB = ActivityStatusDB.getManager(token);
		ActivityStatus planned = null;
		ActivityStatus commited = null;
		ActivityStatus ongoing = null;
		ActivityStatus completed = null;

		try {
			planned = asDB.getByStCode(1);
			ongoing = asDB.getByStCode(2);
			completed = asDB.getByStCode(3);
			commited = asDB.getByStCode(6);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		if (planned == null || ongoing == null || completed == null || commited == null) {
			System.err.println("StatusAutomation$Task.updateStatusByDates: Harmonized Statuses WRONG! (1=planned, 2=ongoing, 3=completed, 6=commited)");
			return null;
		}

		ActivityStatus status = planned;

		long current_date = System.currentTimeMillis();
		if (activity.getDate_ae() <= current_date && activity.getDate_ae() > 0) {
			return completed;
		}

		if (activity.getDate_as() <= current_date && activity.getDate_as() > 0) {
			return ongoing;
		}

		if (activity.getDate_aa() <= current_date && activity.getDate_aa() > 0) {
			return commited;
		}

		return planned;
	}

	public static final String TASK_BEAN_NAME = StatusAutomation$Task.class.getName();
	public static final String TIMER_BEAN_NAME = StatusAutomation$Task.class.getName() + "$Timer";
	public static final String STARTUP_TASK_BEAN_NAME = StatusAutomation$Task.class.getName() + "_STARTUP_";
	public static final String STARTUP_TIMER_BEAN_NAME = StatusAutomation$Task.class.getName() + "$Timer_STARTUP_";
}
